from rest_framework.response import Response


# 封装通过返回结果
# 自定义状态码
class HttpCode(object):
    # 正常登陆
    success = 200
    # 参数错误
    params_error = 400
    # 权限错误
    un_auth = 401
    # 方法错误
    method_error = 405
    # 服务器内部错误
    servererror = 500


# 定义统一的 json 字符串返回格式
def result(code=HttpCode.success, message="操作成功", data=None, kwargs=None):
    json_dict = {"code": code, "message": message, "data": data}
    # isinstance(object对象, 类型):判断是否数据xx类型
    if kwargs and isinstance(kwargs, dict) and kwargs.keys():
        json_dict.update(kwargs)

    return Response(json_dict)


def success():
    return result()


def success_msg(message):
    return result(message=message)


def success_data(data=None):
    return result(data=data)


def success_data_msg(data=None, message=''):
    return result(data=data, message=message)


# 参数错误
def params_error(message="", data=None):
    return result(code=HttpCode.params_error, message=message, data=data)


# 权限错误
def un_auth_error(message="", data=None):
    return result(code=HttpCode.un_auth, message=message, data=data)


# 方法错误
def method_error(message="", data=None):
    return result(code=HttpCode.method_error, message=message, data=data)


# 服务器内部错误
def server_error(message="", data=None):
    return result(code=HttpCode.servererror, message=message, data=data)
